<template >
  <div class='login'>
    <div class="content">
      <div class='left'>
        <div class="logo">
          <img src="../../assets/123.png" alt="">
        </div>
        <h1> 宜春学院<br/>校园招聘系统 </h1>
        <h3>为了更好的未来</h3>
        <p>Copyright @ wangziyu</p>
      </div>
      <div class="right">
        <div class="form">
          <h2>欢迎登录</h2>
          <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }" @submit="handleSubmit">
            <a-form-item label='邮箱' >
              <a-input
                v-decorator="['email', { rules: [{ required: true, message: '请输入邮箱!' }] }]"
                placeholder="邮箱"
              />
            </a-form-item>
            <a-form-item label='密码' >
              <a-input-password
                v-decorator="['password', { rules: [{ required: true, message: '请输入密码!' }] }]"
                placeholder="密码"
              ></a-input-password>
            </a-form-item>
            <a-button type="primary" html-type="submit" class="login-form-button">
              登录
            </a-button>
          </a-form>
          <p>没有账号吗？ 点击 <router-link to="/regist">注册</router-link></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import './styles.less'
import userAPI from '../../services/user'
import { requestWrapper } from '../../utils/request'
export default {
  data() {
    return {
      formLayout: 'inline',
      form: this.$form.createForm(this, { name: 'coordinated' }),
      checked: false,
    };
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          requestWrapper( userAPI.login.bind(null, values), {
              autoSuccess: true,
              successMessage: '登陆成功！',
              errorMessage: '用户名或密码错误！'
          }).then((res) => {
            localStorage.setItem('accessToken', res.token)
            requestWrapper( userAPI.getInfo.bind(null), {
            }).then((res) => {
              console.log(res);
              localStorage.setItem('myInfo', JSON.stringify(res));
              this.$router.push('/home')
            })
          }) 
        }
      });
    },
  },
};

</script>


